package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityNewUserActiveByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-分时累计次留manager
 * 
 * @author xckuang
 * @date 2016-11-13
 */
@Service
public class NewUserActiveByHourManager extends ManagerQueryCacheBase<EntityNewUserActiveByHour> {
	private static final long serialVersionUID = 1L;
	
	//==提供接口支持首页数据
	// 根据平台、渠道、区服更新新增账号分时次留
	public void updateUser2LeftHourByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		updateLookData(updateType,map,getHourUser2LeftByDate(lookDate, queryPlatform, queryChannel, queryServer));
		updateCompData(updateType,map,getHourUser2LeftByDate(compareDate, queryPlatform, queryChannel, queryServer));
		updateWeekData(updateType,map,getHourUser2LeftByDate(lastWeekDate, queryPlatform, queryChannel, queryServer));
	}
	
	/**
	 * 更新lookDate数据，其实可以用反射更漂亮实现 OverviewDetailData的数据结构设计比较诡异
	 * @param updateType
	 * @param map
	 * @param list
	 */
	private void updateLookData(String updateType,Map<Integer, OverviewDetailData> map, List<EntityNewUserActiveByHour> list){
		//判断参数如果异常返回
		if(null == updateType || null == list || list.isEmpty() || null == map || map.isEmpty()){
			return ;
		}
		//更新lookValue
		for(EntityNewUserActiveByHour data : list){
			OverviewDetailData val = map.get(data.getHour());
			if(null != val){
				val.setUserSecond2LeftHourLookValue(data.getUser_second_left());
			}
		}
		
		//由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计
		if("2".equals(updateType)){
			double sum = 0.d;
			for(Map.Entry<Integer, OverviewDetailData> entry : map.entrySet()){
				sum += entry.getValue().getUserSecond2LeftHourLookValue();
				entry.getValue().setUserSecond2LeftHourLookValue(sum);
			}
		}
	}
	
	/**
	 * 更新compDate数据，其实可以用反射更漂亮实现 OverviewDetailData的数据结构设计比较诡异
	 * @param updateType
	 * @param map
	 * @param list
	 */
	private void updateCompData(String updateType,Map<Integer, OverviewDetailData> map, List<EntityNewUserActiveByHour> list){
		//判断参数如果异常返回
		if(null == updateType || null == list || list.isEmpty() || null == map || map.isEmpty()){
			return ;
		}
		
		//更新compareValue
		for(EntityNewUserActiveByHour data : list){
			OverviewDetailData val = map.get(data.getHour());
			if(null != val){
				val.setUserSecond2LeftHourCompareValue(data.getUser_second_left());
			}
		}
		
		//由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计
		if("2".equals(updateType)){
			double sum = 0.d;
			for(Map.Entry<Integer, OverviewDetailData> entry : map.entrySet()){
				sum += entry.getValue().getUserSecond2LeftHourCompareValue();
				entry.getValue().setUserSecond2LeftHourCompareValue(sum);
			}
		}
	}
	
	/**
	 * 更新weekDate数据，其实可以用反射更漂亮实现 OverviewDetailData的数据结构设计比较诡异
	 * @param updateType
	 * @param map
	 * @param list
	 */
	private void updateWeekData(String updateType,Map<Integer, OverviewDetailData> map, List<EntityNewUserActiveByHour> list){
		//判断参数如果异常返回
		if(null == updateType || null == list || list.isEmpty() || null == map || map.isEmpty()){
			return ;
		}
		
		//更新lastweekValue
		for(EntityNewUserActiveByHour data : list){
			OverviewDetailData val = map.get(data.getHour());
			if(null != val){
				val.setUserSecond2LeftHourLastWeekValue(data.getUser_second_left());
			}
		}
		
		//由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计
		if("2".equals(updateType)){
			double sum = 0.d;
			for(Map.Entry<Integer, OverviewDetailData> entry : map.entrySet()){
				sum += entry.getValue().getUserSecond2LeftHourLastWeekValue();
				entry.getValue().setUserSecond2LeftHourLastWeekValue(sum);
			}
		}
	}
	
	/**
	 * 根据日期和平台、渠道、区服参数查询
	 * @param lookDate
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @return
	 */
	public List<EntityNewUserActiveByHour> getHourUser2LeftByDate(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityNewUserActiveByHour where day=:dayStart and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", lookDate, "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
}
